mui.init();
var mask = mui.createMask(); //遮罩层
mui('body').on('tap', '.mui-control-item', function() {
	window.top.location.href = this.href;
	$(this).addClass("mui-active");
});

//点击我的页面
document.getElementById("back").addEventListener("tap", function() {
	window.top.location.href = "my_user.html"
});
mui.back = function() {mui.openWindow({
	url: "my_user.html",
	createNew: true
});}

mui.init({
	pullRefresh: {
		container: "#refreshContainer", //待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
		up: {
			height: 50, // 可选.默认50.触发上拉加载拖动距离
			auto: true, // 可选,默认false.自动上拉加载一次
			contentrefresh: "正在加载...", // 可选，正在加载状态时，上拉加载控件上显示的标题内容
			contentnomore: '没有更多的数据了', // 可选，请求完毕若没有更多数据时显示的提醒内容；
			callback: pullupRefresh //加载数据方法 
		}
	}
})
//mui.ready(function() {
//	mui('.mui-scroll-wrapper').scroll({
//		bounce: true,
//		indicators: true,
//		deceleration: mui.os.ios ? 0.003 : 0.0009
//	});
//});

//获取本地信息
var user_id = localStorage.getItem("user_id");
var pagenum = 1; //页数
var pagesize = 20; //每页条数
var isOver = false; //是否加载完  
function getData() {
	mui.ajax(order_url + '/order/oldOrderList', {
		data: {
			"limit": pagesize,
			"page": pagenum,
			"sign": "string",
			"user_id": user_id
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		headers: {
			'Content-Type': 'application/json'
		},
		success: function(data) {
			if(data.code == 10000) {
				var p = data.total / pagesize; //总页数
				if(pagenum >= p) { //当前的页数
					isOver = true;
					mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
					mui.toast("为您更新了订单" + data.resp.length + "条信息");
				}
				if(isOver == false) {
					pagenum++;
					mui('#refreshContainer').pullRefresh().endPullupToRefresh(true); //停止正在加载
					mui('#refreshContainer').pullRefresh().enablePullupToRefresh(); //显示上拉加载文字
					mui.toast("为您更新了订单" + data.resp.length + "条信息");
				}
				if(data.resp != "") {
					var html = "";
					for(var i = 0; i < data.resp.length; i++) {
						html += '<ul class="mui-table-view orderCon">';
						html += '<li class="mui-table-view-cell">';
						html += '订单号：<span>' + data.resp[i].order_sn + '</span>';
						html += '</li>';
						html += '<li class="mui-table-view-cell orderli">';
						html += '<div class="mui-pull-left">';
						if(data.resp[i].order_state == 0){
							html += '<p>状态：<span class="spanColor">已取消</span></p>';
						}else if(data.resp[i].order_state == 10){
							html += '<p>状态：<span class="spanColor">未付款</span></p>';
						}else if(data.resp[i].order_state == 20){
							html += '<p>状态：<span class="spanColor">已付款</span></p>';
						}else if(data.resp[i].order_state == 30){
							html += '<p>状态：<span class="spanColor">已发货</span></p>';
						}else if(data.resp[i].order_state == 40){
							html += '<p>状态：<span class="spanColor">已收货</span></p>';
						}
						html += '<p>总价：<span>￥<span><span>' + data.resp[i].order_amount  + '</span>';
						html += '</p>';
						html += '<p>时间：<span>' + data.resp[i].add_time + '</span>';
						html += '</p>';
						html += '</div>';
						html += '</li>';
						for(var j = 0; j < data.resp[i].orderDetailsVOS.length; j++) {
							html += '<li class="mui-table-view-cell mui-media">';
							html += '<div class="mui-media-body">';
							html += '<div class="goodsName">' + data.resp[i].orderDetailsVOS[j].goods_name + '</div>';
							html += '<div class="goodsMoney">';
							html += '<span class="mui-pull-left goods_left">￥<span class="goods_left">' + data.resp[i].orderDetailsVOS[j].goods_price + '</span></span>';
							html += '<span class="mui-pull-right goods_right">x<span class="goods_right">' + data.resp[i].orderDetailsVOS[j].goods_num + '</span></span>';
							html += '</div>';
							html += '</div>';
							html += '</li>'; 
						}
						html += '</ul>';
					}
					$("#my_list").append(html);
				} else {
					var html = "";
					$(".mui-content").html('<div style=line-height:400px;text-align:center;color:#C0C0C0;font-size:16px;>暂无商品信息</div>');
				}
			} else {
				mui.alert(data.msg);
			}
		},
		error: function(xhr, type, errorThrown) { //异常处理；
			console.log(type);
		}
	});
}

function pullupRefresh() {
	setTimeout(function() {
		mui('#refreshContainer').pullRefresh().disablePullupToRefresh(false);
		setTimeout(function() {
			getData();
			mui('#refreshContainer').pullRefresh().endPullupToRefresh((isOver)); //参数为true代表没有更多数据了。
		}, 500);
	}, 500);
}

window.onload = function() {
	mui.plusReady(function() {
		var self = plus.webview.currentWebview();
		plus.nativeUI.closeWaiting(); //关闭等待
	});
}

//判断ios
var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
if(isiOS) {
	$("#my_list").css("margin-top", "86px")
}